package com.huaguoguo.cyg.spider.listener;

import com.huaguoguo.cyg.entity.common.dto.SpiderErrorDTO;
import com.huaguoguo.cyg.service.ISpiderErrorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.SpiderListener;

/**
 * 类说明： 抓取失败监听<br/>
 * author:huaguoguo
 * Date: 2020/2/29
 * Time: 18:24
 */
@Component
public class FailSpiderListener implements SpiderListener {

    @Autowired
    private ISpiderErrorService spiderErrorService;

    @Override
    public void onSuccess(Request request) {
        //
    }

    @Override
    public void onError(Request request) {
        // 报错的抓取请求写进数据库
        spiderErrorService.save(SpiderErrorDTO.builder().errorUrl(request.getUrl())
                .errorDetailsText("解析报错").errorType(1).build());
    }
}
